<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-form" label-position="left">
      <!-- logo -->
      <img class="login-logo" src="@/assets/logo.png" alt="">

      <!-- 输入框 -->
      <el-form-item prop="loginName">
        <span class="svg-container el-icon-mobile-phone" />
        <el-input v-model="loginForm.loginName" placeholder="请输入账号" />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container el-icon-lock" />
        <el-input ref="pwd" v-model="loginForm.password" :type="pwdType" placeholder="请输入密码" />
        <span class="svg-container" @click="showPwd">
          <svg-icon :icon-class="pwdType=== 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="code">
        <el-row>
          <el-col :span="17">
            <span class="svg-container el-icon-question" />
            <el-input v-model="loginForm.code" maxlength="4" placeholder="请输入验证码" />
          </el-col>
          <el-col :span="7"><img :src="url" alt="" @click="getImage()"></el-col>
        </el-row>
      </el-form-item>

      <el-button :loading="loading" class="login-btn" type="primary" style="width:100%;margin-bottom:30px;" @click="login">登录</el-button>

    </el-form>
  </div>
</template>

<script>
import { getImageAPI } from '@/api/user'
export default {
  data() {
    return {
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        code: '',
        clientToken: '',
        loginType: 0
      },
      url: '',
      rules: {
        loginName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '密码长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' }
        ]
      },
      pwdType: 'password',
      loading: false
    }
  },
  created() {
    this.getImage()
  },
  methods: {
    // 获取图片验证码
    async getImage() {
      try {
        const num = Math.random() * 100
        const res = await getImageAPI(num)
        this.url = res.config.url
        this.loginForm.clientToken = num
      } catch (error) {
        console.log(error)
      }
    },
    // 点击切换密码框
    showPwd() {
      // 切换
      this.pwdType === 'password' ? this.pwdType = '' : this.pwdType = 'password'
      // 聚焦
      this.$nextTick(() => {
        this.$refs.pwd.focus()
      })
    },
    // 登录
    async login() {
      try {
        await this.$refs.loginForm.validate()
        this.loading = true
        const { data: { msg, success }} = await this.$store.dispatch('user/login', this.loginForm)
        // 成功跳转   失败提示
        if (success) {
          this.$router.push('/')
        } else {
          this.$message.error(msg)
        }
        this.loading = false
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#999;
$cursor: #999;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    width: 100%;
    height: 52px;
    margin-bottom: 24px;
    background: rgb(255, 255, 255);
    border: 1px solid rgb(226, 226, 226);
    border-radius: 4px;
  }

  .el-form-item__error {
    color: #f56c6c;
    font-size: 12px;
    line-height: 1.5;
    padding-top: 4px;
    position: absolute;
    top: 100%;
    left: 0;
}
 .el-row{
      line-height: 10px;
 }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  background: url(~@/assets/background.png);
  background-size: cover;
  background-repeat: no-repeat;
  overflow: hidden;

  .login-form {
    position: absolute;
    width: 518px;
    height: 388px;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
    padding: 76px 35px 0px;
    background: rgb(255, 255, 255);
    box-shadow: rgb(30 111 72 / 35%) 0px 3px 70px 0px;
    border-radius: 10px;
  }

  .login-logo{
    position: absolute;
    width: 96px;
    height: 96px;
    top: -46px;
    left: 50%;
    margin-left: -48px;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .login-btn{
    width: 100%;
    height: 52px;
    background: linear-gradient(262deg, rgb(46, 80, 225), rgb(104, 120, 240));
    opacity: 0.91;
    border-radius: 8px;
    color: rgb(255, 255, 255);
    text-shadow: rgb(207 207 207) 0px 7px 22px;
  }
}
</style>
